//
//  BLTabBarController.m
//  Byelone
//
//  Created by 乔小乔 on 2020/12/14.
//
#import <objc/runtime.h>
#import "BLChatController.h"
#import "BLPartyController.h"
#import "BLPartyContainController.h"
#import "BLMyController.h"
#import "BLInfoAvatarController.h"
#import "BLInfoInputController.h"
#import "BLMeetController.h"
@interface RCDTabBarBtn : UIView

@end

@implementation RCDTabBarBtn
+ (void)load {
    if (@available(iOS 12.1, *)) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class originalClass = NSClassFromString(@"UITabBarButton");
            SEL originalSelector = @selector(setFrame:);
            SEL swizzledSelector = @selector(xp_setFrame:);
            
            Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
            class_replaceMethod(originalClass,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
            class_replaceMethod(originalClass,
                                originalSelector,
                                method_getImplementation(swizzledMethod),
                                method_getTypeEncoding(swizzledMethod));
        });
    }
}

- (void)xp_setFrame:(CGRect)frame {
    if (!CGRectIsEmpty(self.frame)) {
        // for iPhone 8/8Plus
        if (CGRectIsEmpty(frame)) {
            return;
        }
        // for iPhone XS/XS Max/XR
        frame.size.height = MAX(frame.size.height, 48.0);
    }
    [self xp_setFrame:frame];
}

@end



#import "BLTabBarController.h"

@interface BLTabBarController ()<UITabBarControllerDelegate>
@property(nonatomic,assign) NSInteger currentNUm;
@end

@implementation BLTabBarController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    self.tabBar.translucent = NO;
    
    [self.tabBar setShadowImage:[UIImage imageNamed:@"color_white"]];
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"color_white"]];
    if (@available(iOS 13.0, *)) {
        self.tabBar.tintColor = [UIColor chColorWithHexString:@"#6B71F5"];
        self.tabBar.unselectedItemTintColor = [UIColor chColorWithHexString:@"#b0b1b8"];
    } else {
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor chColorWithHexString:@"#b0b1b8"]} forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor chColorWithHexString:@"#6B71F5"]} forState:UIControlStateSelected];
    }
    [self setBarVCs];
}

-(void)setBarVCs{
    self.viewControllers = @[[self meetController],[self partyController],[self chatController],[self myController]];
}

-(UINavigationController*)meetController{
    BLMeetController *meet = [[BLMeetController alloc]init];
    BLNavigationController *navigaiton = [[BLNavigationController alloc] initWithRootViewController:meet];
    navigaiton.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"约会广场" image:[[UIImage imageNamed:@"tab_meet"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_meet_on"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return navigaiton;
}

-(UINavigationController*)partyController{
    BLPartyContainController *party = [[BLPartyContainController alloc] init];
    party.selectIndex = 1;
    party.menuViewStyle = WMMenuViewStyleDefault;
    party.showOnNavigationBar = YES;
    party.menuViewLayoutMode = WMMenuViewLayoutModeLeft;
    party.titleSizeSelected = 20;
    party.titleSizeNormal = 17;
    //    meet.progressWidth = 30;
    party.progressHeight = 4;
    party.titleColorSelected = BL_COLOR_BLUE;
    BLNavigationController *navigaiton = [[BLNavigationController alloc] initWithRootViewController:party];
    navigaiton.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"PARTY" image:[[UIImage imageNamed:@"tab_party"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_party_on"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return navigaiton;
}

-(UINavigationController*)chatController{
    
    BLChatController *chat = [[BLChatController alloc] init];
    BLNavigationController *navigaiton = [[BLNavigationController alloc] initWithRootViewController:chat];
    navigaiton.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"聊天室" image:[[UIImage imageNamed:@"tab_chat"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_chat_on"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return navigaiton;
}

-(UINavigationController*)myController{
    
    BLMyController *my = [[BLMyController alloc] init];
    BLNavigationController *navigaiton = [[BLNavigationController alloc] initWithRootViewController:my];
    navigaiton.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[[UIImage imageNamed:@"tab_my"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_my_on"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return navigaiton;
}

#pragma mark - delegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([viewController isKindOfClass:[BLNavigationController class]]) {
        BLNavigationController *nav = (BLNavigationController*)viewController;
        if (([nav.viewControllers.lastObject isKindOfClass:[BLChatController class]]||[nav.viewControllers.lastObject isKindOfClass:[BLPartyContainController class]]||[nav.viewControllers.lastObject isKindOfClass:[BLMyController class]])&&(![BLUserManager shareUser].userPpu||[[BLUserManager shareUser].registerStep isEqualToString:@"1"]||[[BLUserManager shareUser].registerStep isEqualToString:@"2"])) {//点我的，没有登录跳登录,没有注册完页跳登录
            NSInteger index = 0;
            if ([nav.viewControllers.lastObject isKindOfClass:[BLPartyContainController class]]) {
                index = 1;
            }else if ([nav.viewControllers.lastObject isKindOfClass:[BLChatController class]]) {
                index = 2;
            }else if ([nav.viewControllers.lastObject isKindOfClass:[BLMyController class]]) {
                index = 2;
            }
             
            [BLTool loginWithCompletion:^{
                self.selectedIndex = index;
            }];
            return NO;
        }
//        else if([[BLUserManager shareUser].registerStep isEqualToString:@"1"]){
//            [BLTool getCurrentNavigationController];
//            BLInfoAvatarController *vc = [[BLInfoAvatarController alloc]init];
//            [[BLTool getCurrentNavigationController] pushViewController:vc animated:YES];
//            return NO;
//        }else if ([[BLUserManager shareUser].registerStep isEqualToString:@"2"]){
//            BLInfoInputController *vc = [[BLInfoInputController alloc]init];
//            [[BLTool getCurrentNavigationController] pushViewController:vc animated:YES];
//            return NO;
//        }
    }
    return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSInteger currentIndex = tabBarController.selectedIndex;
    self.currentNUm = currentIndex;
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
}

- (void)dealloc {
    self.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

